﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HotShot.Entities;

namespace HotShot.Client
{
    class Program
    {
        static HotShotClient client = new HotShotClient();

        static void Main(string[] args)
        {
            RunConsole();
        }

        static void RunConsole()
        {
            Dictionary<string, Action<string[]>> verbs = new Dictionary<string, Action<string[]>>
            {
                { "get", GetItem },
                { "put", PutItem },
            };

            Trace.Listeners.Add(new ConsoleTraceListener());
            var s = "foo";
            while (s != "")
            {
                s = Console.ReadLine();
                var ss = s.Split(' ');
                if (ss.Length > 0)
                {
                    Action<string[]> a;
                    if (verbs.TryGetValue(ss[0].ToLower(), out a))
                    {
                        a(ss);
                    }
                }
            }
        }

        static void GetItem(string[] args)
        {
            var r = client.Retrieve(args[1]);
            Trace.WriteLine(r.Payload);
        }

        static void PutItem(string[] args)
        {
            var entity = new StorageEntity
            {
                Key = args[1],
                Payload = args[2],
                Ticks = DateTime.UtcNow.Ticks,
            };

            client.Store(entity);
        }

    }
}
